<template>
  <div>
     <breadcrumb></breadcrumb>
    <!-- 搜索表单 -->
    <!-- @submit.native.prevent停止默认行为 @submit是提交-->
    <el-form :inline="true" class="demo-form-inline"  @submit.native.prevent>
      <el-form-item label="商品名称">
        <el-input v-model="name"  placeholder="请输入商品名称" @keyup.native.enter="listproByName"></el-input>
      </el-form-item>
      <el-form-item>
        <!-- 点击回车 -->
        <el-button type="primary" @click="listproByName"  >查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 分割线 -->
    <el-divider content-position="left">商品列表</el-divider>

    <!-- 表格 -->
    <template style="table-layout:fixed">
      <el-table
        :data="pro.result"
        style="width: 100%;">
        <el-table-column
          prop="westerndetail_id"
          label="编号"
          align="center"
          width="100">
        </el-table-column>
        <el-table-column
          prop="title"
          label="商品名称"
          align="center"
          width="180"
         >
        </el-table-column>
        <el-table-column
          label="商品图片"
          width="150">
          <template slot-scope="scope">
            <img v-show="!scope.row.img1" src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png" alt="" width="60px">
            <img v-if="imgnone" v-src="scope.row.img1" width="60px">
            <img v-else :src="scope.row.img1" alt="" width="60px">
          </template>
        </el-table-column>
        <el-table-column
          prop="explains"
          label="商品介绍"
          align="center"
          :show-overflow-tooltip="true"
          width="350"
        >
        </el-table-column>
          <el-table-column  align="center" label="操作">
          <template slot-scope="scope">
            <el-button type="success" icon="el-icon-picture" circle  @click="$router.push(`/home/pro-details/${scope.row.westerndetail_id}`)"></el-button>
            <el-button type="primary" icon="el-icon-edit" circle @click="$router.push(`/home/pro-revise/${scope.row.westerndetail_id}`)"></el-button>
            <el-button type="danger" icon="el-icon-delete" @click="DeleteItem(scope.row.westerndetail_id)" circle></el-button>
          </template>           
        </el-table-column>
      </el-table>
    </template>

    <br>
    <!-- 分页 -->
    <el-pagination
      background
      :total=pro.total
      :page-size=pro.pagesize
      layout=" ->,prev, pager, next,total,jumper"
      @current-change="ChangCurrentPage"
    >
    </el-pagination>

 
    
  </div>
</template>

<script>
import breadcrumb from "../components/breadcrumb.vue";
import tables from "../components/tables.vue";
import httpApi from "@/http";
  export default {
    components: { breadcrumb,tables },
     data(){
        return{
          name:'',
          pro:{
            page: 1,
            pagesize:5,
            result:[],
            total:1
          },
          imgnone: true,
        }
     },
    methods:{
      // 删除店铺
      DeleteItem(westerndetail_id){
        // 弹窗
        this.$confirm('此操作将永久删除用户信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 确定
          console.log(westerndetail_id);
          httpApi.proApi.deleteProById({westerndetail_id}).then(res =>{
            if(res.data.code == 200){
              // 提醒用户删除成功
              this.$message({ type: 'success', message: '删除成功!' });
              // 刷新
              this.search()
            }else{
              // 提醒用户删除失败
              this.$message.error('错了哦，删除失败');
            }
          })
        }).catch(() => {
            this.$message({ type: 'info', message: '已取消删除' });          
          });
        },

      // 点击查询按钮后执行，需要判断输入框中是否有关键字
      search(){
        //trim()是去掉字符串两端的空格
          if(this.name.trim() == ''){
            //当无输入值
            this.$store.state.utype == 1 ?  this.listPro() : this.listStorePro()
          }else{
            //有输入值
            this.listproByName()
            
          }
        },
        // 封装系统管理员商品列表
      listPro(){
          // let url = "http://localhost:3000/pro/list?page=1&pagesize=100"
          let params = {page: this.pro.page ,pagesize: this.pro.pagesize}
          httpApi.proApi.queryAllProList(params).then(res =>{
            // console.log("商品列表",res);
            this.pro = res.data.data
            this.pro.result.map((item,index)=>{
              var reg=/^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\/])+$/;
                 if(reg.test(this.pro.result[index].img1)){
                   this.imgnone = false
                }
            })
          })
        },
        // 封装店铺商品
      listStorePro(){
          let params = {page: this.pro.page ,pagesize: this.pro.pagesize,store_uid: this.$store.state.uid}
          httpApi.proApi.queryAllProListByStroe(params).then(res =>{
            // console.log("商品列表",res);
            this.pro = res.data.data
            this.pro.result.map((item,index)=>{
              var reg=/^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\/])+$/;
                 if(reg.test(this.pro.result[index].img1)){
                   this.imgnone = false
                }
            })
          })
        },

      //变化当前页
      ChangCurrentPage(page){
          // console.log("当前页数",page);
          this.pro.page = page
          this.search()
        },

        // 通过昵称关键字查询演员列表
      listproByName(){
          let params = {title:this.name.trim(),page:this.pro.page, pagesize: this.pro.pagesize}
            httpApi.proApi.queryProrByNameLike(params).then(res =>{
              // console.log("商品模糊查询",res);
              this.pro = res.data.data
              this.pro.result.map((item,index)=>{
              var reg=/^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\/])+$/;
                 if(reg.test(this.pro.result[index].img1)){
                   this.imgnone = false
                }
            })
              
            })     
          }
        },

      // 页面挂载完毕后，发送请求，加载列表数据
      mounted(){
        if(this.$store.state.utype == 1){
          //页面挂在完毕后，发送请求，加载列表数据
          this.listPro()
        }else{
          this.listStorePro()
        }
        
      }
  }
</script>

<style lang="scss" scoped>

  .el-tooltip__popper {
  max-width: 60px;
}


</style>